package com.xuan.yrmanage.common.redis;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * FileName: StringDateRedisUtils
 * Author:   xuan zongjun
 * Date:     2020/03/13
 * Description: spring data redis redis工具类
 */

@Service
public class StringDateRedisUtils {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    /**
     * 存入hash值
     *
     * @param key
     * @param value
     * @return
     */
    public void insertRedisKeyValue(String key, String value, long timeout) {
        try {
            //设置过期时间，单位秒
            if (timeout > 0) {
                stringRedisTemplate.opsForValue().set( key,value,timeout );
            } else {
                stringRedisTemplate.opsForValue().set( key,value );
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }




    /**
     * 存入hash值
     *
     * @param key  hash类型的key
     * @param hashKey hash类型中hash的key
     * @return
     */
    public void insertHashRedisDate(String key, String hashKey, Object object, long timeout) {
        try {
            //设置过期时间，单位秒
            if (timeout > 0) {
                stringRedisTemplate.opsForHash().put( key,hashKey,object.toString() );
                stringRedisTemplate.expire(key,timeout, TimeUnit.SECONDS   );
                Long expire = stringRedisTemplate.getExpire( key );
                System.out.println(expire);
            } else {
                stringRedisTemplate.boundHashOps( key ).put( hashKey, object );
            }


        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 获取值，返回列表值
     *
     * @param hashKey
     * @param key
     * @return
     */
    public List<T> getHashRedisList(String hashKey, String key, Class<T> clazz) {
        try {
            List<T> list = new ArrayList();
            Object object = stringRedisTemplate.boundHashOps( hashKey ).get( key );
            List objectlist = JSON.parseObject( object.toString(), List.class );
            for (Object obj : objectlist) {
                T vo = JSON.parseObject( obj.toString(), clazz );
                list.add( vo );
            }
            return list;
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 获取值，返回列表值
     *
     *
     * @param key
     * @return
     */
    public String getRedisValueByKey(String key) {
        try {
            String value = stringRedisTemplate.opsForValue().get( key );
            return value;
        } catch (Exception e) {
            return null;
        }
    }




    /**
     * 获取对象值
     *
     * @param hashKey
     * @param key
     * @return Object
     */
    public Object getHashRedisDate(String key, String hashKey) {
        try {
            return stringRedisTemplate.boundHashOps( key ).get( hashKey );
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 删除，如果hashKey不传入，整个hash，如果指定hashKey，清空hash中指定的的hashKey
     */
    public boolean deleteHashRedis(String key, String hashKey) {
        try {
            if (StringUtils.isNotEmpty( hashKey )) {
                stringRedisTemplate.boundHashOps( key ).delete( hashKey );
            } else {
                stringRedisTemplate.delete( key );
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }


    /**
     * 获取key的过期时间
     */
    public long getHashRedisExpire(String key) {
        try {
            return stringRedisTemplate.getExpire( key );
        } catch (Exception e) {
            return 0;
        }
    }


}
